package com.zto.wk.webflux.handler;

import com.zto.wk.webflux.user.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.Mono;

/**
 * Created by wangkuan at 2018-08-02 14:20
 */
@Component
public class EchoHandler implements WebSocketHandler {

    @Autowired
    private UserManager userManager;
    int index = 0;

    @Override
    public Mono<Void> handle(WebSocketSession session) {
        String id = session.getId();
        System.out.println("id=" + id);

        userManager.add(id, session, "user" + ++index);

        return session.send(session.receive()
                .map(msg -> session.textMessage("服务端返回 -> " + msg.getPayloadAsText())));
    }
}
